﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.LogicalGroups.CApLGREditGroupsManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.DataTypes;


namespace DnProcessSimulateCommands.LogicalGroups;

internal class CApLGREditGroupsManager
{
  private TxTypeFilter m_compoundsFilterIn;
  private TxTypeFilter m_compoundsFilterOut;

  internal CApLGREditGroupsManager()
  {
    this.m_compoundsFilterIn = new TxTypeFilter();
    this.m_compoundsFilterIn.AddIncludedType(typeof (TxCompoundPart));
    this.m_compoundsFilterIn.AddIncludedType(typeof (TxCompoundResource));
    this.m_compoundsFilterIn.AddIncludedType(typeof (ITxLogicalGroup));
    this.m_compoundsFilterOut = new TxTypeFilter();
    this.m_compoundsFilterOut.AddIncludedType(typeof (ITxLogicalChild));
    this.m_compoundsFilterOut.AddExcludedType(typeof (TxCompoundPart));
    this.m_compoundsFilterOut.AddExcludedType(typeof (TxCompoundResource));
  }

  internal void Flatten(TxSelectionInformationEntry simpleEntry)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    ITxObject selectedObject = simpleEntry.SelectedObject;
    TxLogicalGroup fromSelectionInfo = this.GetParentObjectFromSelectionInfo(simpleEntry);
    TxApplication.ActiveUndoManager.StartTransaction();
    try
    {
      this.FlattenObject(selectedObject, fromSelectionInfo);
    }
    catch (TxException ex)
    {
    }
    finally
    {
      TxApplication.ActiveUndoManager.EndTransaction();
      Cursor.Current = current;
    }
    this.UpdateCurrentSelection(selectedObject, fromSelectionInfo);
  }

  private void FlattenObject(ITxObject obj, TxLogicalGroup parent)
  {
    if (obj is TxLogicalGroup)
      this.FlattenGroup(obj as TxLogicalGroup);
    else
      this.FlattenCompound(obj as ITxCompound, parent);
  }

  private void FlattenGroup(TxLogicalGroup lgGroup)
  {
    if (lgGroup == null)
      return;
    foreach (ITxObject directDescendant in (Collection<ITxObject>) lgGroup.GetDirectDescendants((ITxTypeFilter) this.m_compoundsFilterIn))
      this.FlattenObject(directDescendant, lgGroup);
  }

  private void FlattenCompound(ITxCompound iTxCompound, TxLogicalGroup parent)
  {
    if (!(iTxCompound is ITxObjectCollection objectCollection))
      return;
    TxObjectList allDescendants = objectCollection.GetAllDescendants((ITxTypeFilter) this.m_compoundsFilterOut);
    for (int index = ((Collection<ITxObject>) allDescendants).Count - 1; index >= 0; --index)
    {
      if (!parent.CanBeAdded(((Collection<ITxObject>) allDescendants)[index]) || CApLGRLogicalGroupsUtil.IsSubComponent(((Collection<ITxObject>) allDescendants)[index]))
        ((Collection<ITxObject>) allDescendants).RemoveAt(index);
    }
    try
    {
      parent.RemoveObject((ITxObject) iTxCompound);
      if (((Collection<ITxObject>) allDescendants).Count <= 0)
        return;
      parent.AddObjects(allDescendants);
    }
    catch (TxException ex)
    {
    }
  }

  internal void ReplaceCompoundsWithGroups(TxSelectionInformationEntry simpleEntry)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    ITxObject selectedObject = simpleEntry.SelectedObject;
    TxLogicalGroup fromSelectionInfo = this.GetParentObjectFromSelectionInfo(simpleEntry);
    TxApplication.ActiveUndoManager.StartTransaction();
    try
    {
      this.ReplaceObject(selectedObject, fromSelectionInfo);
    }
    catch (TxException ex)
    {
    }
    finally
    {
      TxApplication.ActiveUndoManager.EndTransaction();
      Cursor.Current = current;
    }
    this.UpdateCurrentSelection(selectedObject, fromSelectionInfo);
  }

  private void ReplaceObject(ITxObject obj, TxLogicalGroup txLogicalGroup)
  {
    if (obj is TxLogicalGroup)
    {
      this.ReplaceGroup(obj as TxLogicalGroup);
    }
    else
    {
      TxLogicalGroup lgGroup = this.ReplaceCompound(obj as ITxCompound, txLogicalGroup);
      if (lgGroup == null)
        return;
      this.ReplaceGroup(lgGroup);
    }
  }

  private void ReplaceGroup(TxLogicalGroup lgGroup)
  {
    if (lgGroup == null)
      return;
    foreach (ITxObject directDescendant in (Collection<ITxObject>) lgGroup.GetDirectDescendants((ITxTypeFilter) this.m_compoundsFilterIn))
      this.ReplaceObject(directDescendant, lgGroup);
  }

  private TxLogicalGroup ReplaceCompound(ITxCompound txCompound, TxLogicalGroup parent)
  {
    TxLogicalGroup txLogicalGroup = (TxLogicalGroup) null;
    if (txCompound != null)
    {
      TxObjectList directDescendants = (txCompound as ITxObjectCollection).GetDirectDescendants((ITxTypeFilter) new TxNoTypeFilter());
      try
      {
        parent.RemoveObject((ITxObject) txCompound);
        TxLogicalGroupCreationData groupCreationData = new TxLogicalGroupCreationData(((ITxObject) txCompound).Name);
        txLogicalGroup = parent.CreateLogicalGroup(groupCreationData);
        for (int index = ((Collection<ITxObject>) directDescendants).Count - 1; index >= 0; --index)
        {
          if (!txLogicalGroup.CanBeAdded(((Collection<ITxObject>) directDescendants)[index]))
            ((Collection<ITxObject>) directDescendants).RemoveAt(index);
        }
        if (((Collection<ITxObject>) directDescendants).Count > 0)
          txLogicalGroup.AddObjects(directDescendants);
      }
      catch (TxException ex)
      {
        txLogicalGroup = (TxLogicalGroup) null;
      }
    }
    return txLogicalGroup;
  }

  private TxLogicalGroup GetParentObjectFromSelectionInfo(TxSelectionInformationEntry simpleEntry)
  {
    TxLogicalGroup fromSelectionInfo = (TxLogicalGroup) null;
    if (!(simpleEntry.SelectedObject is TxLogicalGroup) && simpleEntry is TxSelectionTreeInformationEntry informationEntry && informationEntry.Parent != null)
      fromSelectionInfo = informationEntry.Parent as TxLogicalGroup;
    return fromSelectionInfo;
  }

  private void UpdateCurrentSelection(ITxObject selObject, TxLogicalGroup parent)
  {
    TxObjectList txObjectList = new TxObjectList(1);
    if (selObject is TxLogicalGroup)
      ((Collection<ITxObject>) txObjectList).Add(selObject);
    else if (parent != null)
      ((Collection<ITxObject>) txObjectList).Add((ITxObject) parent);
    if (((Collection<ITxObject>) txObjectList).Count <= 0)
      return;
    TxApplication.ActiveSelection.SetItems(txObjectList);
  }
}
